package cn.sxt.bus.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.sxt.bus.domain.Outport;
import cn.sxt.bus.domain.Provider;
import cn.sxt.bus.service.OutportService;
import cn.sxt.bus.service.ProviderService;
import cn.sxt.bus.vo.OutportVo;
import cn.sxt.bus.vo.ProviderVo;
import cn.sxt.sys.utils.DataGridView;

@Controller
@RequestMapping("outport")
public class OutportController {

	@Autowired
	private OutportService outportService;
	
	@Autowired
	private ProviderService providerService;

	// 跳到outportManager.jsp
	@RequestMapping("toOutportManager")
	public String toOutportManager() {
		return "business/outport/outportManager";
	}

	// outportManager.jsp加载 左边
	@RequestMapping("toOutportLeft")
	public String toOutportLeft() {
		return "business/outport/outportLeft";
	}

	// outportManager.jsp加载 右边
	@RequestMapping("toOutportRight")
	public String toOutportRight() {
		return "business/outport/outportRight";
	}

	// 进货数据，分页，模糊
	@RequestMapping("queryAllOutport")
	@ResponseBody
	public DataGridView queryAllOutport(OutportVo outportVo) {
		return outportService.queryAllOutport(outportVo);
	}

	// 跳添加页面
	@RequestMapping("toAddOutport")
	public String toAddOutport(Model model) {
		List<Provider> list = providerService.queryAllproviderServiceForList(new ProviderVo());
		model.addAttribute("providers", list);
		return "business/outport/outportAdd";
	}

	// 添加
	@RequestMapping("addOutport")
	@ResponseBody
	public Map<String, Object> addOutport(OutportVo outportVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			outportVo.setOutputtime(new Date());
			int i = outportService.addOutport(outportVo);
			if (i > 0) {
				msg = "添加成功";
			} else {
				msg = "添加失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "添加失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 跳修改页面，查回显信息
	@RequestMapping("toUpdateOutport")
	public String toUpdateOutport(OutportVo outportVo, Model model) {
		OutportVo outport = outportService.queryOutportVoById(outportVo.getId());
		model.addAttribute("outport", outport);
		List<Provider> list = providerService.queryAllproviderServiceForList(new ProviderVo());
		model.addAttribute("providers", list);
		return "business/outport/outportUpdate";
	}

	// 修改
	@RequestMapping("updateOutport")
	@ResponseBody
	public Map<String, Object> updateOutport(OutportVo outportVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = outportService.updateOutport(outportVo);
			if (i > 0) {
				msg = "修改成功";
			} else {
				msg = "修改失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "修改失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 删除
	@RequestMapping("deleteOutport")
	@ResponseBody
	public Map<String, Object> deleteOutport(OutportVo outportVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = outportService.deleteOutportById(outportVo);
			if (i > 0) {
				msg = "删除成功";
			} else {
				msg = "删除失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 批量删除商品
	@RequestMapping("batchDeleteOutport")
	@ResponseBody
	public Map<String, Object> batchDeleteOutport(OutportVo outportVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			Integer[] ids = outportVo.getIds();
			if (ids != null && ids.length > 0) {
				for (int i = 0; i < ids.length; i++) {
					outportVo.setId(ids[i]);
					int j = outportService.deleteOutportById(outportVo);
					if (j > 0) {
						msg = "删除成功";
					} else {
						msg = "删除失败";
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

}
